<?php
$langid			= empty($_GET['langid'])	? 0 : intval($_GET['langid']);
$libraryid		= empty($_GET['libraryid'])	? 0 : intval($_GET['libraryid']);
$orderby = get_major_key(array('id' => 'DESC'));
$_f_librarys	= cache_read('librarys.php');
$oSentence = new Sentence();

if(!empty($_GET['btnExport'])){	// 导出
	
	$list = $oSentence->get_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $key => $row){
			$row['pack']		= get_pack($row['libraryid']);
			$row['lang']		= $__f_langs['lang_id_name_list'][$row['langid']];
			$row['library']		= $_f_librarys['library_list'][$row['libraryid']]['name'];
			$row['addtime']		= ftime($row['addtime']);
			$row['updatetime']	= ftime($row['updatetime']);
			$data[] = $row;
		}
		$headers = array(
			'id'			=> array('title' => 'ID',		'width' => '4'),
			'lang'			=> array('title' => '语言',		'width' => '6'),
			'library'		=> array('title' => '模块',		'width' => '6'),
			'pack'			=> array('title' => '包',		'width' => '12.5'),
			'mark'			=> array('title' => '标识',		'width' => '25'),
			'content'		=> array('title' => '内容',		'width' => 'auto', 'wrap' => true),
			'addtime'		=> array('title' => '添加时间',	'width' => '18'),
			'updatetime'	=> array('title' => '更新时间',	'width' => '18'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_词句_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_词句_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

$oLibrary = new Library();
$library_tree = $oLibrary->get_library_tree_ul();	// 取分类树


$list = $oSentence->get_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list']) && is_array($list['list'])){
	// 主键排序字段
	make_major_key_options(array(
		'id'		=> '添加时间',
		'langid'	=> '语言',
		'libraryid'	=> '模块',
	));
	foreach($list['list'] as $key => $row){
		$row['pack']		= get_pack($row['libraryid']);
		$row['lang']		= $__f_langs['lang_id_name_list'][$row['langid']];
		$row['library']		= $_f_librarys['library_list'][$row['libraryid']]['name'];
		$row['addtime']		= ftime($row['addtime']);
		$row['updatetime']	= ftime($row['updatetime']);
		$list['list'][$key] = $row;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}

// 保持搜索参数
$vars['langid_options'] = make_options($__f_langs['lang_id_name_list'], true, $langid);
$vars['library_tree'] = $library_tree;
if($libraryid > 0){
	$vars['library_name'] = $_f_librarys['library_list'][$libraryid]['name'];
}else{
	$vars['library_name'] = $__l['root'];
}


function get_pack($libraryid){
	global $_f_librarys;
	$library = $_f_librarys['library_list'][$libraryid];
	if($library['pid'] > 0){
		$path = get_pack($library['pid']).'_';
	}else{
		$path = '';
	}
	return $path.$library['mark'];
}
?>